package com.edu.service.impl;

import com.edu.dao.CheckItemDao;
import com.edu.entity.PageResult;
import com.edu.entity.QueryPageBean;
import com.edu.pojo.CheckItem;
import com.edu.service.CheckItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        // 完成分页查询,基于 pagehelper 来实现
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> pages = checkItemDao.selectByItem(queryString);
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    public void deleteById(Integer id) {
        long countByCheckItemId = checkItemDao.findCountByCheckItemId(id);
        if(countByCheckItemId>0){
            new RuntimeException();
        }
        checkItemDao.deleteById(id);
    }

    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
